package com.heima.wemedia.listen;


import com.heima.common.constants.wemedia.NewsAutoScanConstants;
import com.heima.wemedia.service.WmNewsAutoScanService;
import com.heima.wemedia.service.WmNewsService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
@Slf4j
public class WemediaNewAutoListener {
    @Autowired
    WmNewsAutoScanService wmNewsAutoScanService;

    @Autowired
    WmNewsService wmNewsService;

    /**
     *
     * @param newsId
     * @param message
     * @param channel
     */

    @RabbitListener(queuesToDeclare = {@Queue(name = NewsAutoScanConstants.WM_NEWS_AUTO_SCAN_TOPIC)})
    public void newsAutoScanHandler(@Payload String newsId, Message message, Channel channel){
        log.info("接收到 自动审核 消息===> {}",newsId);
        try {
            // 自动审核
            wmNewsAutoScanService.autoScanWmNews(Integer.valueOf(newsId));
            // 手动确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            log.error(" 消费消息出现异常  ， 原因: {}" ,e.getMessage());
        }
    }

}
